<template>
  <div class="page-mobile-test">
    <van-tabs v-model="active" sticky @change="handleChange">
      <van-tab name="my-test" :title="$t('locale.mobile.test.t1')">
        <div class="my-test">
          <div class="no-data" v-if="course_record_list.length == 0">
            <van-empty :description="$t('locale.mobile.test.t2')" />
          </div>
          <!-- <div class="tap-container" ref="target">
            <div
              class="tap-wrap"
              ref="tap"
              :style="{ transform: `translate3D(${left}px,0,0)` }"
            >
              <div
                :class="['tap-item', item % 2 == 0 ? 'tap-item-active' : null]"
                v-for="item in 10"
              >
                高等数学
              </div>
            </div>
          </div> -->
          <div class="card-test-box">
            <test-card
              v-for="(item, index) in course_record_list"
              :key="index"
              :detail="item"
              :type="1"
            ></test-card>
          </div>
        </div>
      </van-tab>
      <van-tab name="test-center" :title="$t('locale.mobile.test.t3')">
        <div class="no-data" v-if="paper_list.length == 0">
          <van-empty :description="$t('locale.mobile.test.t4')" />
        </div>
        <div class="test-center" v-if="paper_list.length > 0">
          <div class="card-test-box">
            <exam-card
              v-for="(item, index) in paper_list"
              :key="index"
              :detail="item"
            ></exam-card>
          </div>
        </div>
      </van-tab>
      <van-tab name="wrong-note" :title="$t('locale.mobile.test.t5')">
        <div class="no-data" v-if="course_record_list.length == 0">
          <van-empty :description="$t('locale.mobile.test.t2')" />
        </div>
        <div class="wrong-note" v-if="buy_record_list.length > 0">
          <div class="card-test-box">
            <test-card
              v-for="(item, index) in course_record_list"
              :key="index"
              :detail="item"
              :type="2"
            ></test-card>
          </div>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
import { draggable } from "~/utils/commonfun";
import TestCard from "~/components/mobile/TestCard.vue";
import ExamCard from "~/components/mobile/ExamCard.vue";
import { getAllPaper, getPaperMine, wrongQuestions } from "~/api/test";
import { getOrder, getOrderList } from "~/api/order";
export default {
  scrollToTop: true,
  components: {
    TestCard,
    ExamCard
  },
  head() {
    return {
      meta: [
        // {
        //   "http-equiv": "Content-Security-Policy",
        //   content: "upgrade-insecure-requests"
        // }
      ]
    };
  },
  props: ["setDisabled", "refreshFun", "setLoading"],
  data() {
    return {
      active: "my-test",
      left: 0,
      paper_list: [] //试卷列表
    };
  },
  computed: {
    ...mapState({
      user_info(state) {
        return state.user_info;
      },
      major_list(state) {
        return state.major_list;
      },
      buy_record_list(state) {
        return state.buy_record_list;
      },
      course_record_list(state) {
        return state.course_record_list;
      }
    }),
    tap() {
      return this.$route.query.tap;
    }
  },
  activated() {
    if (process.client) {
      if (!this.token && Object.keys(this.user_info).length == 0) {
        this.$router.replace("/m/login");
        return;
      }
    }
    if (this.tap) {
      this.active = this.tap;
    }
    this.setDisabled(false);
    this.refreshFun(() => {
      this.initTest();
      this.setLoading(false);
    });
  },
  deactivated() {
    this.setDisabled(true);
  },
  watch: {
    course_record_list(new_val, old_val) {
      console.log("new_val:", new_val);
      console.log("old_val:", old_val);
      // if (new_val.length !== old_val.length) {
      //   this.getTestPapers(
      //     new_val.map(i => {
      //       return {
      //         ...i,
      //         courseId: i.id
      //       };
      //     })
      //   );
      // }
    }
  },
  methods: {
    ...mapActions(["initMajorList", "initBuyCourseRecord", "initBuyRecord"]),
    ...mapMutations(["SET_IS_LOADING", "SET_COURSE_RECORD_LIST"]),
    handleDragMoving({ x, y, params }) {
      let left = x + params;
      let targetWidth = this.$refs.tap.offsetWidth;
      let tragWidth = this.$refs.target.offsetWidth;
      if (left < -(targetWidth - tragWidth)) {
        left = -(targetWidth - tragWidth);
      }
      if (left >= 0) {
        left = 0;
      }
      this.left = left;
    },
    async getTestPapers(buy_record_list) {
      this.SET_IS_LOADING(false);
      let params = {
        courseId: JSON.stringify(buy_record_list.map(i => i.courseId))
      };
      let result = await getPaperMine(params);
      if (result && result.data && result.data.code == 200) {
        this.paper_list = result.data.data;
        this.SET_IS_LOADING(false);
      } else {
        this.$toast(this.$t("locale.mobile.test.t6"));
        this.SET_IS_LOADING(false);
      }
    },
    async getCoursesMine() {
      this.SET_IS_LOADING(true);
      let params = {
        userId: this.user_info.id
      };
      let result = await getOrderList(params);
      if (result && result.data && result.data.code == 200) {
        this.SET_COURSE_RECORD_LIST(result.data.data);

        this.getTestPapers(
          result.data.data.map(i => {
            return {
              ...i,
              courseId: i.courseId
            };
          })
        );
      } else {
        this.$toast(this.$t("locale.mobile.test.t6"));
        this.SET_IS_LOADING(true);
      }
    },
    handleChange(name) {
      this.$router.replace({
        path: "",
        query: {
          tap: name
        }
      });
    },
    initTest() {
      if (process.client) {
        if (!this.token && Object.keys(this.user_info).length == 0) {
          this.$router.replace("/m/login");
          return;
        } else {
          this.initMajorList();
          this.getCoursesMine();
          if (this.tap) {
            this.active = this.tap;
          }
        }
      }
    }
  },
  created() {
    this.setDisabled(false);
    this.initTest();
    this.refreshFun(() => {
      this.initTest();
      this.setLoading(false);
    });
  },
  mounted() {
    this.$nextTick(() => {
      if (process.client && this.buy_record_list.length > 0) {
        // let drag_target = this.$refs.tap;
        // console.log(drag_target);
        // draggable(drag_target, {
        //   start: () => {
        //     return this.left;
        //   },
        //   moving: this.handleDragMoving
        // });
      }
    });
  }
};
</script>

<style lang="scss" scoped>
.page-mobile-test {
}
</style>
<style lang="scss">
.page-mobile-test {
  min-height: calc(100vh - 1.6rem);
  background-color: #f9f8f8;
  padding: 0 0 0.2rem 0;
  .van-sticky--fixed {
    top: 0.8rem;
  }
  .card-test-box {
  }
  .my-test {
    padding: 0.2rem 0;
    .tap-container {
      display: flex;
      align-items: center;
      width: 7.2rem;
      height: 0.8rem;
      overflow: hidden;
      .tap-wrap {
        padding: 0.1rem;
        white-space: nowrap;
        .tap-item {
          background-color: #ecebeb;
          display: inline-block;
          padding: 0.1rem 0.2rem;
          border-radius: 1rem;
          color: #101010;
          font-size: 0.22rem;
        }
        .tap-item + .tap-item {
          margin-left: 0.2rem;
        }
        .tap-item-active {
          background-color: var(--theme-color);
          color: #fff;
        }
      }
    }
  }
  .test-center {
    padding: 0.2rem 0;
  }
  .wrong-note {
    padding: 0.2rem 0;
  }
}
</style>
